//
//  SetViewController.m
//  HQJFacilitator
//
//  Created by mymac on 16/9/27.
//  Copyright © 2016年 Fujian first time iot technology investment co., LTD. All rights reserved.
//

#import "SetViewController.h"
#import "LoginViewController.h"
#import "ChangePswViewController.h"
#import "ChangeMobileViewController.h"
#import "SetTableViewCell.h"
#import "AboutViewController.h"

#define KsystemInformsStr  "http://www.ww1000.cn/OMobile/inotice.html"
#define KHelpStr  "http://www.ww1000.cn/OMobile/ihelp.html"
#define KaboutStr "http://www.ww1000.cn/OMobile/iabout.html"
//static const NSString *KsystemInformsStr =@"http://www.ww1000.cn/OMobile/about.html";
//static const NSString *KHelpStr =@"http://www.ww1000.cn/OMobile/about.html";
//static const NSString *KaboutStr =@"http://www.ww1000.cn/OMobile/about.html";

@interface SetViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *setTableView;
@property (nonatomic,strong)NSArray *titleAry;
@property (nonatomic,strong)UILabel *backLoginLabel;
@property (nonatomic,strong)UILabel *versionLabel;
@end

@implementation SetViewController
-(UITableView *)setTableView {
    if (!_setTableView) {
        
        
        
        _setTableView = [[UITableView alloc]init];
        _setTableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
        _setTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _setTableView.delegate = self;
        
        _setTableView.dataSource = self;
        _setTableView.sectionFooterHeight = 2;
        _setTableView.sectionHeaderHeight = 2;
        [_setTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableView class])];
        [_setTableView registerClass:[SetTableViewCell class] forCellReuseIdentifier:NSStringFromClass([SetTableViewCell class])];
//        _setTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _setTableView.tableFooterView = [UIView new];
        
        
        
        
        
    }
    
    
    
    return _setTableView;
}

-(UILabel *)backLoginLabel {
    if (!_backLoginLabel) {
        _backLoginLabel = [[UILabel alloc]init];
        _backLoginLabel.font = [UIFont systemFontOfSize:17];
        _backLoginLabel.frame = CGRectMake(0, 0, WIDTH, 44);
        _backLoginLabel.textColor = [UIColor redColor];
        _backLoginLabel.text = @"退出登录";
        _backLoginLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _backLoginLabel;
}

-(UILabel *)versionLabel {
    
    if (!_versionLabel) {
        _versionLabel = [[UILabel alloc]init];
        _versionLabel.font = [UIFont systemFontOfSize:12];
        _versionLabel.textColor = [ManagerEngine getColor:@"999999"];
        _versionLabel.text = [NSString stringWithFormat:@"v%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
        
        
    }
    
    
    return _versionLabel;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    
        return 3;

    
   
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
        if (section == 0) {
            return 3;
        } else if (section == 1) {
            
            return 3;
        } else {
            
            return 1;
            
        }

}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section !=2) {
        return 10;
    } else {
        
        return 30;
    }
    
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    return view;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    
    
    SetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SetTableViewCell class]) forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.section == 0) {
   
        cell.textLabel.text = [self setindexAry:indexPath][indexPath.row];
        /***右侧小箭头***/
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if (indexPath.row == 2) {
//            cell.lineView.alpha = 0;
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, MAXFLOAT)];

        }
        
    } else if (indexPath.section ==1){
     
        cell.textLabel.text = [self setindexAry:indexPath][indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        if (indexPath.row == 2) {
            [cell addSubview:self.versionLabel];
            CGFloat versionSize =[ManagerEngine setTextWidthStr:self.versionLabel.text andFont:[UIFont systemFontOfSize:12]];
            self.versionLabel.sd_layout.leftSpaceToView(cell,WIDTH-30-versionSize).topSpaceToView(cell,(44-12)/2).heightIs(12).widthIs(versionSize);
//            cell.lineView.alpha = 0;
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, MAXFLOAT)];

        }
    } else {
//        cell.lineView.alpha = 0;
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, MAXFLOAT)];

        [cell addSubview:self.backLoginLabel];
    }
    return cell;
    
    
    
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {

        if (indexPath.row == 0) {
           [self goChangePswTitle:@"登录密码修改"];
        }else if (indexPath.row == 1)  {
           [self goChangePswTitle:@"交易密码修改"];
        } else {
            ChangeMobileViewController *CVC = [[ChangeMobileViewController alloc]init];
            CVC.title = @"手机号码修改";
            [self.navigationController pushViewController:CVC animated:YES];
        }

    } else if (indexPath.section==1) {
        AboutViewController *AVC = [[AboutViewController alloc]init];

        switch (indexPath.row) {
            case 0:
                AVC.title = @"系统通知";
                AVC.urlWebStr = @KsystemInformsStr;
                break;
            case 1:
                AVC.title = @"帮助与反馈";
                AVC.urlWebStr = @KHelpStr;

                break;
            case 2:
                AVC.title = @"关于";
                AVC.urlWebStr = @KaboutStr;

                break;
            default:
                break;
        }
        [self.navigationController pushViewController:AVC animated:YES];

    } else {
        LoginViewController *loginVC =[[LoginViewController alloc]init];
        ZWNavigationController *Nav = [[ZWNavigationController alloc]initWithRootViewController:loginVC];
        [self.view.window addSubview:Nav.view];
        Nav.view.frame = CGRectMake(0, HEIGHT, WIDTH, HEIGHT);
        [UIView animateWithDuration:0.75 animations:^{
            
            Nav.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
            
            
        } completion:^(BOOL finished) {
            [Nav.view removeFromSuperview];
            self.view.window.rootViewController =Nav;

        }];
//    [self presentViewController:loginVC animated:YES completion:nil];

    }
         
    
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.setTableView];
   
  
    

}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.setTableView reloadData];
    
      [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginAfter) name:@"changeBonus" object:nil];
    
}



-(void)loginAfter {
    
    [self.navigationController popViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(NSArray *)setindexAry:(NSIndexPath *)index {
    
    if (index.section == 0) {
        return @[@"登录密码修改",
                 @"交易密码修改",
                 @"手机号码修改"];
    } else if (index.section == 1) {
        
        return @[@"系统通知",
                 @"帮助与反馈",
                 @"关于"];
    } else {
        
        
        
        return nil;
    }
    
}

#pragma mark --
#pragma mark --- 去修改密码界面
-(void)goChangePswTitle:(NSString *)tit{
    ChangePswViewController *CVC = [[ChangePswViewController alloc]init];
    CVC.title = tit;
    [self.navigationController pushViewController:CVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
